package edu.ucsd.cse222b.network;

import java.util.ArrayList;
import java.util.List;

import org.jgrapht.graph.DefaultWeightedEdge;

public class Link extends DefaultWeightedEdge {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private double bandwidth;
	private double availableBandwidth;
	private List<NetFlow> flowsThrough=new ArrayList<NetFlow>();
	
	public Link(){
		super();
	}
	public void setBandwidth(double b){
		bandwidth=b;
		availableBandwidth=b;
	}
	public double getBandwidth(){
		return bandwidth;
	}
	public double getAvailableBandwidth(){
		return availableBandwidth;
	}
	public double getCost(){
		return getWeight();
	}
	public void addFlow(NetFlow flow){
		flowsThrough.add(flow);
	}
	public void removeFlow(NetFlow flow){
		flowsThrough.remove(flow);
	}
	public List<NetFlow> getFlowsThrough(){
		return flowsThrough;
	}
	public String toString(){
		return "e"+"("+getSource()+","+getTarget()+","+getWeight()+","+bandwidth+")";
	}
	public NetNode getSrcNode(){
		return (NetNode) getSource();
	}
	
	public NetNode getDstNode(){
		return (NetNode) getTarget();
	}
	
	public boolean allocBandwidth(double b){
		if(b<=availableBandwidth){
			availableBandwidth-=b;
			return true;
		}
		return false;
	}
	public int getNumOfUnsaturatedFlows() {
		int result=0;
		for(NetFlow flow : flowsThrough){
			if(flow.isSaturated()==false){
				result++;
			}
		}
		return result;
	}
	
	public double getUtilizedBandwidth(){
		double utilizedBandwidth=0;
		for(NetFlow flow : flowsThrough){
			utilizedBandwidth+=flow.getCurrentBandwidth();
		}
		return utilizedBandwidth;
	}
	
}
